﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports DotNetNuke
Namespace NVPortal.Modules.NVNews

    Public MustInherit Class View
        Inherits Entities.Modules.PortalModuleBase
        Public Function ReplaceChuoi(ByVal chuoi As String) As String
            Dim convert As String = Global.ReplaceChuoi.bodau(chuoi).Replace(" ", "-")
            Return convert
        End Function

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '
            If Not IsPostBack Then
                Try
                    BindGridData()
                Catch ex As Exception
                    ProcessModuleLoadException(Me, ex)
                End Try
            End If
        End Sub
#End Region

        Private Sub BindGridData()
            Dim ctlNewsCategory As New NV_NewsCategoriesController
            Dim arrNewsCategories As New ArrayList
            arrNewsCategories = ctlNewsCategory.GetAll(PortalId)
            Dim arrTemp As New ArrayList
            Dim objNewsCategories As NV_NewsCategoriesInfo
            Dim objNewsCategoriesTemp As NV_NewsCategoriesInfo
            Dim objNewsCategoriesTemp3 As NV_NewsCategoriesInfo

            If arrNewsCategories.Count > 0 Then
                For Each objNewsCategories In arrNewsCategories
                    If objNewsCategories.ParentId = 0 Then
                        arrTemp.Add(objNewsCategories)
                        For Each objNewsCategoriesTemp In arrNewsCategories
                            If objNewsCategoriesTemp.ParentId = objNewsCategories.CategoryID Then
                                objNewsCategoriesTemp.CategoryName = "||---- " & objNewsCategoriesTemp.CategoryName
                                arrTemp.Add(objNewsCategoriesTemp)

                                For Each objNewsCategoriesTemp3 In arrNewsCategories
                                    If objNewsCategoriesTemp3.ParentId = objNewsCategoriesTemp.CategoryID Then
                                        objNewsCategoriesTemp3.CategoryName = "||----||---- " & objNewsCategoriesTemp3.CategoryName
                                        arrTemp.Add(objNewsCategoriesTemp3)
                                    End If
                                Next

                            End If
                        Next
                    End If
                Next
            End If

            Me.drgViewData.DataSource = arrTemp
            Me.drgViewData.DataBind()
        End Sub

        Private Sub lbtAddBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtAddBottom.Click, lbtAddTop.Click
            Try
                Response.Redirect("/TabID/" & TabId & "/view/add/default.aspx", True)
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        'Protected Sub drgViewData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles drgViewData.ItemDataBound
        '    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        '        Dim objTab As New NV_NewsCategoriesInfo
        '        Dim objTabCtl As New NV_NewsCategoriesController
        '        objTab = objTabCtl.GetByID(CType(Me.drgViewData.DataKeys(e.Item.ItemIndex), Integer))

        '        If Not objTab Is Nothing Then

        '            If objTab.ParentId > 0 Then
        '                e.Item.BackColor = Color.White
        '                e.Item.Font.Bold = False
        '            End If

        '            If objTab.ParentId = 0 Then
        '                e.Item.Font.Bold = True
        '                e.Item.BackColor = Color.LightYellow
        '            End If

        '            ''Neu la Tab con cua Tab khac
        '            'If objTab.ParentId > 0 Then

        '            '    Dim StrTabName As String = "|------ "
        '            '    StrTabName = StrTabName & e.Item.Cells(0).Text
        '            '    'Gan them xau |------ vao dau de phan biet
        '            '    e.Item.Cells(0).Text = StrTabName
        '            'End If

        '        End If
        '    End If
        'End Sub

        Protected Sub lbtUpdateOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtUpdateOrder.Click
            Dim objDataRow As DataGridItem
            Dim ctlNewsCategories As New NV_NewsCategoriesController
            For Each objDataRow In Me.drgViewData.Items
                If (objDataRow.ItemType = ListItemType.Item) Or (objDataRow.ItemType = ListItemType.AlternatingItem) Then
                    ' update data base
                    ctlNewsCategories.UpdateOrderNumber(Me.drgViewData.DataKeys(objDataRow.ItemIndex).ToString, CType(objDataRow.FindControl("txtOrderNumber"), TextBox).Text)

                End If
            Next
            ' bind grid
            BindGridData()
        End Sub
    End Class

End Namespace
